import random
import mod_gui,mod_verbs,mod_actor,mod_item,mod_direction
from mod_verbs import Verb
from mod_ai import AIBrain

# Defines the slug character and things relating to it.

class GooTrapSteppedOnEffect:
    def doIt(self,actor,gooTrap):
        actor.takeDamage(2,'acid')
        mod_gui.theGUI.messages.append(actor.glyph+' STEPS ON GOO!')

gooPrototype = mod_item.ItemPrototype('GOO',grabbable=False)
gooPrototype.steppedOnEffect = GooTrapSteppedOnEffect()

class Poop(Verb):
    def __init__(self):
        Verb.__init__(self,'POOP','BUTTON_POOP',AP=2)
    
    def doIt(self,actor,item,target):
        actor.useAP(self.calculateAP(actor,item,target))
        location = actor.location
        newPoop = gooPrototype.instantiate()
        newPoop.setLocation(location)

poopAbility = Poop()

class SlugBrain(AIBrain):
    def getPoopAbility(self):
        for ability in self.actor.abilities:
            if isinstance(ability,Poop):
                return ability
        return None
    
    def takeTurn(self):
        AIBrain.takeTurn(self)
        # Check if there's already poop here:
        levelMemory = self.memory.getLevelMemory(self.actor.location[0].number)
        
        gooHere = False
        itemsHere = levelMemory.itemMap.get(self.actor.location[1])
        if itemsHere:
            for item in itemsHere:
                if item.prototype == gooPrototype:
                    gooHere = True
                    break
        if not gooHere:
            poopAbility = self.getPoopAbility()
            poopAbility.doIt(self.actor,None,None)
        else:
            dir = random.choice(mod_direction.ALLDIRS)
            self.move(dir)

slugPrototype = mod_actor.ActorPrototype('SLUG', maxHP=1, maxAP=3, APperTurn=3, brainPrototype=SlugBrain)
slugPrototype.abilities.append(poopAbility)
slugPrototype.resistanceMap['acid']=0.0
